package com.le.tester.javaThink.fifth.chapter24_ConcurrentProgramming;

import java.util.concurrent.CompletableFuture;

/**
 * createTime：2021/8/20 15:40
 * description：CompletableOperations
 */
public class CompletableOperations {
    static CompletableFuture<Integer> cfi(int i) {
        return CompletableFuture.completedFuture(Integer.valueOf(i));
    }

    public static void main(String[] args) {
        CompletableUtilities.showr(cfi(1));
        CompletableUtilities.voidr(cfi(2).runAsync(() -> System.out.println("runAsync")));
        CompletableUtilities.voidr(cfi(3).thenRunAsync(() -> System.out.println("thenRunAsync")));
        CompletableUtilities.voidr(CompletableFuture.runAsync(() -> System.out.println("run async is static")));
        CompletableUtilities.showr(CompletableFuture.supplyAsync(() -> 99));
        CompletableUtilities.voidr(cfi(4).thenAcceptAsync(i -> System.out.println("then accept async:" + i)));
        CompletableUtilities.showr(cfi(5).thenApplyAsync(i -> i + 42));

        CompletableUtilities.showr(cfi(6).thenComposeAsync(i -> cfi(i + 99)));

        CompletableFuture<Integer> c = cfi(7);
        c.obtrudeValue(111);

        CompletableUtilities.showr(c);

        CompletableUtilities.showr(cfi(8).toCompletableFuture());

        c = new CompletableFuture<>();

        c.complete(9);

        CompletableUtilities.showr(c);

        c = new CompletableFuture<>();

        System.out.println(c.getNow(777));

        c = new CompletableFuture<>();

        c.thenApplyAsync(i -> i + 42).thenApplyAsync(i -> i * 12);

        System.out.println("dependents: " +
                c.getNumberOfDependents());

        c.thenApplyAsync(i -> i / 2);
        System.out.println("dependents: " +
                c.getNumberOfDependents());

    }
}
